package com.xiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiao.domain.constants.ArticleCategoryConstants;
import com.xiao.domain.ResponseResult;
import com.xiao.domain.dto.category.AddArticleCategoryDto;
import com.xiao.domain.dto.category.ArticleCategoryListDto;
import com.xiao.domain.dto.category.SaveUpdateCategoryDto;
import com.xiao.domain.vo.PageVo;
import com.xiao.domain.vo.category.ArticleCategoryListVo;
import com.xiao.enums.AppHttpCodeEnum;
import com.xiao.mapper.ArticleCategoryMapper;
import com.xiao.domain.pojo.ArticleCategory;
import com.xiao.service.ArticleCategoryService;
import com.xiao.utils.BeanCopyUtils;
import com.xiao.domain.vo.category.ArticleCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service // 别忘记 注入Bean
public class ArticleCategoryServiceImpl extends ServiceImpl<ArticleCategoryMapper, ArticleCategory> implements ArticleCategoryService {

    @Autowired
    ArticleCategoryService articleCategoryService;

    @Override
    public ResponseResult getCategoryList() { // 这里用了  stream 来操作数据 感觉可能还是写 sql 来的清楚效率高。后续可以改

        // 1. 编写条件
        LambdaQueryWrapper<ArticleCategory> wrapper = new LambdaQueryWrapper<>();
        // 分类状态正常
        wrapper.eq(ArticleCategory::getStatus, ArticleCategoryConstants.STATUS_NORMAL); // mybatis_plus 写法: 代表表的字段 status ArticleCategory::getStatus

        // 2. 提交查询
        List<ArticleCategory> list = articleCategoryService.list(wrapper); // 分类状态正常 的结果


        // 3. 提取分类Id 并且去重; Set 集合特点 无序无下标元素不可重复 所以用来去重 再好不过
        Set<Long> setIds = list.stream()// 其实没看懂, 因为id 是主键本身不会重复
                .map(articleCategory -> articleCategory.getId())
                .collect(Collectors.toSet());// 这个集合就是 分类状态正常的分类ID
// 4. 根据 分类ID 查询分类
        List<ArticleCategory> articleCategoryList = this.listByIds(setIds); // ServiceImpl 父类的方法

        List<ArticleCategory> filterArticleCategoryList = articleCategoryList.stream()
                // 过滤 提取状态正常的分类
                .filter((articleCategory) -> ArticleCategoryConstants.STATUS_NORMAL.equals(articleCategory.getStatus()))
                .collect(Collectors.toList());

        //转化成sql语句 就是这样 逻辑有问题;第一 id 本身唯一不需要剔除，第二 name 会重复但是没剔除重复的name SELECT*FROM `blog_category` WHERE id IN (SELECT DISTINCT id FROM `blog_category` WHERE STATUS ='0') AND STATUS ='0'
        // 5. 使用 Vo 封装
        List<ArticleCategoryVo> result = BeanCopyUtils.copyBeanList(filterArticleCategoryList, ArticleCategoryVo.class);

        return ResponseResult.okResult(result);// 通过我们自己的响应进行包装
    }

    @Override
    public ResponseResult categoryList(Integer pageNum, Integer pageSize, ArticleCategoryListDto dto) {
        // 1. 构建条件
        LambdaQueryWrapper<ArticleCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(dto.getStatus()),ArticleCategory::getStatus,dto.getStatus());
        wrapper.like(StringUtils.hasText(dto.getName()),ArticleCategory::getName,dto.getName());
        // 2. 分页
        Page<ArticleCategory> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        Page<ArticleCategory> categoryPage = this.page(page, wrapper);

        // 3. 转 vo
        PageVo pageVo = new PageVo(categoryPage.getRecords(), page.getTotal());
        // 4. 统一响应
        ResponseResult responseResult = ResponseResult.okResult(pageVo);

        return responseResult;
    }

    @Override
    public ResponseResult addArticleCategory(AddArticleCategoryDto dto) {

        // 1. 插入数据
        ArticleCategory articleCategory = BeanCopyUtils.copyBean(dto, ArticleCategory.class);
        boolean save = this.save(articleCategory);
        // 2. 统一响应

        ResponseResult responseResult;
        if (save){
            responseResult = ResponseResult.okResult();
        }else {
            responseResult=ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }

        return responseResult;
    }

    @Override
    public ResponseResult getCategoryById(Long id) {
        ArticleCategory articleCategory = this.getBaseMapper().selectById(id);
        ArticleCategoryListVo vo = BeanCopyUtils.copyBean(articleCategory, ArticleCategoryListVo.class);
        ResponseResult responseResult =ResponseResult.okResult(vo);
        return responseResult;
    }

    @Override
    public ResponseResult saveUpdateByDto(SaveUpdateCategoryDto dto) {
        // 1. 保存修改
        ArticleCategory articleCategory = BeanCopyUtils.copyBean(dto, ArticleCategory.class);

        boolean isUpdate = this.updateById(articleCategory);
        // 2. 统一响应
        ResponseResult responseResult;
        if (isUpdate){
            responseResult=ResponseResult.okResult();
        }else{
            responseResult=ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }

        return responseResult;
    }

    @Override
    public ResponseResult delCategoryByIdList(List<Long> idList) {
        int i = this.getBaseMapper().deleteBatchIds(idList);
        ResponseResult responseResult;
        if (i>0){
            responseResult = ResponseResult.okResult();
        }else{
            responseResult = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        return responseResult;
    }
}
